﻿using System;
using System.Collections.Generic;
using System.Data;
using NBearLite;
using NBear.Mapping;
using Xin478.CSWork.Common;

namespace Xin478.CSWork.DomainModel
{
    public class ActionModel
    {
        public static Dictionary<string, ActionInfo> GetEmployActions(Database db,int employId,bool withGroupActions)
        {
            Dictionary<string, ActionInfo> actions = null;
            IDataReader dr;
            /*=db.Select(MyWork.Actions)
                .Join(MyWork.Action_Employee, MyWork.Actions.Id == MyWork.Action_Employee.ActionId)
                .Where(MyWork.Action_Employee.EmployeeId == employId)
                .ToList<ActionInfo>();*/

            if (withGroupActions)
            {
                 dr = db.Select(MyWork.Employees_Group , MyWork.Actions.Id, MyWork.Actions.Action, MyWork.Action_Group.Rights )
                    .Join(MyWork.Action_Group, MyWork.Employees_Group.GroupId == MyWork.Action_Group.GroupId)
                    .Join(MyWork.Actions, MyWork.Action_Group.ActionId == MyWork.Actions.Id)
                    .Where(MyWork.Employees_Group.EmployeeId == employId)
                    .ToDataReader();
                if (dr.Read())
                {
                    if (actions == null) actions = new Dictionary<string, ActionInfo>();
                    do
                    {
                        ActionInfo ai=new ActionInfo();
                        ai.Rights=Convert.ToByte(dr["rights"]);
                        ai.Id=Convert.ToInt32(dr["id"]);
                        ai.Action=dr["action"].ToString();
                        actions.Add(ai.Action, ai);
                    } while (dr.Read());
                    dr.Close();
                }
            }

            dr = db.Select(MyWork.Actions)
                .Join(MyWork.Action_Employee, MyWork.Actions.Id == MyWork.Action_Employee.ActionId)
                .Where(MyWork.Action_Employee.EmployeeId == employId)
                .ToDataReader();
            if (dr.Read())
            {
                if (actions == null) actions = new Dictionary<string, ActionInfo>();
                do
                {
                    ActionInfo ai = new ActionInfo();
                    ai.Rights = Convert.ToByte(dr["rights"]);
                    ai.Id = Convert.ToInt32(dr["id"]);
                    ai.Action = dr["action"].ToString();
                    actions[ai.Action] = ai;
                } while (dr.Read());
                dr.Close();
            }
            return actions;
        }
        public static IList<ActionInfo> GetGroupActions(int groupId)
        {
            return new Database("MyWork").Select(MyWork.Actions).Join(MyWork.Action_Group, MyWork.Actions.Id == MyWork.Action_Group.ActionId).Where(MyWork.Action_Group.GroupId == groupId).ToList<ActionInfo>();
        }

        public static ActionInfo GetAction(int actionid)
        {
            return new Database("MyWork").Select(MyWork.Actions,MyWork.Actions.Action).Where(MyWork.Actions.Id==actionid).ToSingleObject<ActionInfo>();
        }


    }
}